# -*- coding: utf-8 -*-
# @Time    : 2019/10/24 10:05
# @Author  : LiHaowen
# @Email   : lihaowen2017@163.com
# @File    : thread_queue_test.py

# 通过queue的方法进行线程间同步

from queue import Queue
import time
import threading


total = 0


def get_detail_html(queue):
    """ 获取文章详情页 """
    while True:
        if not queue.empty():
            url = queue.get()  # 阻塞方法
            # for url in detail_url_list:
            print("get detail html started")
            time.sleep(1)
            print(url)
            time.sleep(1)
            print("get detail html end")
            global total
            total += 1
        if total == 20:  # 数据总量，跳出循环条件
            break


def get_detail_url(queue):
    """ 获取文章列表页 """
    while True:
        print("get detail url started")
        time.sleep(1)
        for i in range(20):
            queue.put(f"http://projects.edu.com/{i}")
        print("get detail url end")
        break


if __name__ == '__main__':
    detail_url_queue = Queue(maxsize=1000)
    thread1 = threading.Thread(target=get_detail_url,
                               args=(detail_url_queue,))  # threading类实例化只能传递函数名，参数使用tuple传递
    threads = []
    for i in range(10):
        html_thread = threading.Thread(target=get_detail_html, args=(detail_url_queue,))
        html_thread.start()
        threads.append(html_thread)

    start_time = time.time()
    thread1.start()
    thread1.join()
    # detail_url_queue.task_done()
    # detail_url_queue.join()  # 阻塞主线程
    for j in threads:
        j.join()


    print(f"last time: {time.time() - start_time}")


# def worker():
#     while True:
#         item = q.get()
#         if item is None:
#             break
#         do_work(item)
#         q.task_done()
#
# q = queue.Queue()
# threads = []
# for i in range(num_worker_threads):
#     t = threading.Thread(target=worker)
#     t.start()
#     threads.append(t)
#
# for item in source():
#     q.put(item)
#
# # block until all tasks are done
# q.join()
#
# # stop workers
# for i in range(num_worker_threads):
#     q.put(None)
# for t in threads:
#     t.join()